<template>
  <div class="{{name}}-container">
    {{!-- <vanpeople-query-form>
      <vanpeople-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">添加</el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">批量删除
        </el-button>
      </vanpeople-query-form-left-panel>
      <vanpeople-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.id" placeholder="请输入查询条件" clearable />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary">查询
            </el-button>
          </el-form-item>
        </el-form>
      </vanpeople-query-form-right-panel>
    </vanpeople-query-form> --}}
    <div>
      <el-form ref="queryForm" :model="queryForm" label-width="80px" inline @submit.native.prevent>
        <el-form-item label="租户名称/ID" prop="name" label-width="100px">
          <el-input v-model="queryForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          <el-button icon="el-icon-refresh-left" type="primary" @click="resetForm('queryForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin: 10px 0px">
      <el-button icon="el-icon-circle-plus-outline" type="primary" @click="handleEdit">创建</el-button>
    </div>
    <div>
      <el-table v-loading="listLoading" :data="list" border :element-loading-text="elementLoadingText"
        @selection-change="setSelectRows">
        {{!-- <el-table-column type="selection"></el-table-column> --}}
        <el-table-column prop="id" label="id" />
        <el-table-column fixed="right" label="操作" width="200">
          <template v-slot="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button type="text" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination  style="text-align: right;" background :current-page="queryForm.pageNo" :page-size="queryForm.pageSize" :layout="layout"
        :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
      <edit ref="edit" @fetchData="fetchData"></edit>

    </div>
  </div>
</template>

<script>

  import { getList, doDelete } from "@/api/{{name}}";
  import Edit from "./components/{{properCase name}}Edit";

  export default {
    name: '{{ properCase name }}',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: "total, sizes, prev, pager, next, jumper",
        total: 0,
        selectRows: "",
        elementLoadingText: "正在加载...",
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          id: "",
          name:""
        },
      };
    },
    created() {
      this.fetchData();
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val;
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs["edit"].showEdit(row);
        } else {
          this.$refs["edit"].showEdit();
        }

      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm("你确定要删除当前项吗", null, () => {
            doDelete({ ids: row.id }).then((res) => {
              this.$baseMessage(res.msg, "success");
              this.fetchData();
            });
          });
        } else {
         
          
            this.$baseConfirm(
              "你确定要删除选中项吗",
              null,
              () => {
                doDelete({ ids: row.id }).then((res) => {
                  this.$baseMessage(res.msg, "success");
                  this.fetchData();
                });
              },
            );
        
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val;
        this.fetchData();
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val;
        this.fetchData();
      },
      queryData() {
        this.queryForm.pageNo = 1;
        this.fetchData();
      },
      fetchData() {
        this.listLoading = true;
        getList(this.queryForm).then(res => {
          this.list = res.data.rows;
          this.total = res.data.total;
          setTimeout(_ => {
            this.listLoading = false;
          }, 300);
        });
      },
      resetForm(ref) {
        this.$refs[ref].resetFields();
         this.fetchData();
      }
    },
  };
</script>
<style lang="scss" scoped></style>